<?php

namespace App\Service;

use App\Models\OtherInfo;
use Carbon\Carbon;

class OtherInfoService
{
    /**
     * 其他信息列表
     * @param array $params
     * @return array
     */
    public function list(array $params): array
    {
        $query = OtherInfo::query();

        // 查询 user_name booking_time store_name script_name script_details
        $query->select('user_id', 'user_name', 'title', 'details', 'state', 'end_time', 'remark', 'weight', 'created_at');

        // 精确搜索 user_id
        if (!empty($params['user_id'])) {
            $query->where('user_id', $params['user_id']);
        }

        // 模糊搜索
        if (!empty($params['search'])) {
            $search = $params['search'];
            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%$search%");
                $q->orWhere("details", "like", "%$search%");
            });
        }

        // 状态搜索
        if (!empty($params['state'])) {
            $query->where('state', $params['state']);
        }

        // end_time 匹配（范围筛选）
        if (!empty($params['date'])) {
            $date      = $params['date'];
            $startTime = Carbon::parse($date)->startOfDay();
            $endTime   = Carbon::parse($date)->endOfDay();
            $query->whereBetween('end_time', [$startTime, $endTime]);
        }

        $query->where('end_time', '>=', Carbon::now());
        // 排序：创建时间升序，权重降序
        $query->orderBy('created_at', 'asc')->orderBy('weight', 'desc');
        $page     = max(1, intval($params['page'] ?? 1));
        $pageSize = max(1, intval($params['page_size'] ?? 10));
        $total    = $query->count();
        $list     = $query->forPage($page, $pageSize)->get()->toArray();
        return [
            'list'      => $list,
            'total'     => $total,
            'page'      => $page,
            'page_size' => $pageSize,
        ];
    }

    /**
     * 新增其他信息
     * @param array $params
     * @return array
     */
    public function create(array $params): array
    {
        $data = [
            'user_id'   => $params['user_id'] ?? '',
            'user_name' => $params['user_name'] ?? '',
            'title'     => $params['title'] ?? '',
            'details'   => $params['details'] ?? '',
            'state'     => $params['state'] ?? '',
            'remark'    => $params['remark'] ?? '',
            'weight'    => $params['weight'] ?? 0,
            'end_time'  => $params['end_time'] ?? null,
        ];
        // unique_no 自动生成
        $data['unique_no'] = md5("{$params['user_id']}_{$params['title']}_{$params['title']}_{$params['title']}_{$params['title']}");
        $otherInfo         = OtherInfo::query()->create($data);
        return $otherInfo ? $otherInfo->toArray() : [];
    }

    /**
     * 软删除其他信息
     * @param int $id
     * @return void
     */
    public function delete(int $id): void
    {
        $info = OtherInfo::query()->find($id);
        if ($info) {
            $info->delete();
        }
    }
}
